﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.DirectX.DirectSound;
using System.Diagnostics;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for ConfigControl.xaml
    /// </summary>
    public partial class ToolsControl : UserControl
    {
        private delegate void VoidInvokeHandler();
        Recorder jane;
        Burner max;
        Congregation congregation;
        Sharing sharing;
        MainWindow mainWindow;

        public ToolsControl(Recorder _jane, Burner _max, Congregation _congregation, Sharing _sharing, MainWindow _window)
        {
            jane = _jane;
            max = _max;
            congregation = _congregation;
            sharing = _sharing;
            mainWindow = _window;
            InitializeComponent();
        }

        void ExitSoundServantButton_Click(object sender, RoutedEventArgs e)
        {
            if (!mainWindow.IsCloseable)
            {
                SSMessageBox.Show("Cannot close Sound Servant", mainWindow.IsntCloseableReason);
            }
            else if (SSMessageBox.Show("Close Sound Servant?", "Are you sure you want to close Sound Servant?", SSMessageBoxType.YesNo) == SSMessageBoxResult.Yes)
            {

                if (((Process[])Process.GetProcessesByName("Explorer")).Length == 0)
                {
                    Process.Start("Explorer");
                }
                mainWindow.CloseSoundServant();
            }
        }

        private void SnapshotSkypeButton_Click(object sender, RoutedEventArgs e)
        {
            if (sharing != null)
            {
                bool wasExplorerRunning = true;
                if (((Process[])Process.GetProcessesByName("Explorer")).Length == 0)
                {
                    wasExplorerRunning = false;
                    Process.Start("Explorer");
                }
                bool returnToWindowedMode = SS.FullScreenMode;
                SS.FullScreenMode = false;
                SnapshotSkypeWindow.Show(sharing, congregation);
                SS.FullScreenMode = returnToWindowedMode;

                if (!wasExplorerRunning)
                {
                    if (((Process[])Process.GetProcessesByName("Explorer")).Length > 0) ((Process[])Process.GetProcessesByName("Explorer"))[0].Kill();
                    SS.FullScreenMode = true;
                }
            }
        }

        private void ReinitializeButton_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.Tidy();
            mainWindow.Initialize();
        }

        private void RestartButton_Click(object sender, RoutedEventArgs e)
        {
            if (SSMessageBox.Show("Restart Sound Servant", "Are you sure you want to restart Sound Servant?", SSMessageBoxType.YesNo) == SSMessageBoxResult.Yes)
            {
                Process.Start(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
                ((Process)Process.GetCurrentProcess()).Kill();
            }
        }

        private void ViewLogFilesButton_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("Explorer", Options.Default.BaseDirectory + "\\" + Options.Default.LogDirectory);
        }

        private void ChangePasswordButton_Click(object sender, RoutedEventArgs e)
        {
            ChangePasswordWindow.Show(congregation);
        }

        private void RunExplorerButton_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("Explorer");
            SS.FullScreenMode = false;
        }

        private void KillExplorerButton_Click(object sender, RoutedEventArgs e)
        {
            ((Process[])Process.GetProcessesByName("Explorer"))[0].Kill();
            SS.FullScreenMode = true;

        }
    }
}
